import pics_manager
import argparse
import cmd
import os

class PicsManagerCmdUI(cmd.Cmd):
    intro = 'Welcome to PicsManager command utility. Type "help" or "?" to list commands.\n'
    prompt = 'PicsManager > '

    def __init__(self, pm):
        cmd.Cmd.__init__(self, completekey='tab')
        self.pm = pm

    def do_show_labels(self, args):
        '''show all the labels'''
        labels = pm.getPicsLabelInfo()
        print(labels)

    def do_quit(self, args):
        '''quit, bye, exit'''
        return True

    def do_import(self, args):
        '''import one picture into manager. Format: import <pic_path> <label> [label ...]'''
        strs = args.split(' ')
        if len(strs) < 2:
            print('you must specify at least on label for a picture')
            return
        if os.path.exists(strs[0]) == False:
            print('file not exist: ', strs[0])
        self.pm.setPicLabels(strs[0], strs[1:])

    def do_find(self, args):
        '''find pics by labels'''
        labels = args.split(' ')
        pic_names = self.pm.getPicsByLabels(labels)
        print(pic_names)

    def do_show_all(self, args):
        '''show all pics and its labels'''
        ret = self.pm.getAllPicsLabels()
        print(ret)
        
if __name__ == '__main__':
    ap = argparse.ArgumentParser('PicsManager')
    ap.add_argument('--dir', required=True, action='store', help='data dir')
    args = ap.parse_args()

    pm = pics_manager.PicsManager()
    pm.load(args.dir)
    cmd_ui = PicsManagerCmdUI(pm)
    cmd_ui.cmdloop()

    print('save data...')
    pm.save()
    print('bye~')
    print()
